package com.zhangsun.service.impl;

import com.zhangsun.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * redis服务
 */
@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @SuppressWarnings("unchecked")
    public boolean expire(String key, long timeout, TimeUnit unit) {
        Boolean ret = redisTemplate.expire(key, timeout, unit);
        return ret != null && ret;
    }

    @Override
    @SuppressWarnings("unchecked")
    public boolean del(String key) {
        Boolean ret = redisTemplate.delete(key);
        return ret != null && ret;
    }

    @Override
    @SuppressWarnings("unchecked")
    public long del(Collection<String> keys) {
        Long ret = redisTemplate.delete(keys);
        return ret == null ? 0 : ret;
    }

    @Override
    @SuppressWarnings("unchecked")
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    @SuppressWarnings("unchecked")
    public void set(String key, Object value, long timeout) {
        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }

    @Override
    @SuppressWarnings("unchecked")
    public void set(String key, Object value, long timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    @Override
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    @SuppressWarnings("unchecked")
    public void hashPut(String key, String hKey, Object value) {
        redisTemplate.opsForHash().put(key, hKey, value);
    }

    @Override
    @SuppressWarnings("unchecked")
    public void hashPutAll(String key, Map<String, Object> values) {
        redisTemplate.opsForHash().putAll(key, values);
    }

    @Override
    @SuppressWarnings("unchecked")
    public Object hashGet(String key, String hKey) {
        return redisTemplate.opsForHash().get(key, hKey);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Object> hashMultiGet(String key, Collection<Object> hKeys) {
        return redisTemplate.opsForHash().multiGet(key, hKeys);
    }

    @Override
    @SuppressWarnings("unchecked")
    public long setSet(String key, Object... values) {
        Long count = redisTemplate.opsForSet().add(key, values);
        return count == null ? 0 : count;
    }

    @Override
    @SuppressWarnings("unchecked")
    public long setDel(String key, Object... values) {
        Long count = redisTemplate.opsForSet().remove(key, values);
        return count == null ? 0 : count;
    }

    @Override
    @SuppressWarnings("unchecked")
    public long listPush(String key, Object value) {
        Long count = redisTemplate.opsForList().rightPush(key, value);
        return count == null ? 0 : count;
    }

    @Override
    @SuppressWarnings("unchecked")
    public long listPushAll(String key, Collection<Object> values) {
        Long count = redisTemplate.opsForList().rightPushAll(key, values);
        return count == null ? 0 : count;
    }

    @Override
    @SuppressWarnings("unchecked")
    public long listPushAll(String key, Object... values) {
        Long count = redisTemplate.opsForList().rightPushAll(key, values);
        return count == null ? 0 : count;
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Object> listGet(String key, int start, int end) {
        return redisTemplate.opsForList().range(key, start, end);
    }

}
